home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 008a / fgdemo10.zip / CHAR.C < prev    next >
Text File  |  1991-10-02  |  14KB  |  542 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  char.c -- character display and management routines                 *
  4. *                                                                      *
  5. \**********************************************************************/
  6.  
  7. #include "defs.h"
  8.  
  9. char *font;
  10. char *pfont;
  11. int  ptsize;
  12. int  *spacing;
  13.  
  14. int spacing_08pt[] = {
  15. /* !  "  #  $  %  &  '  (  )  *  +  ,  - */
  16.    3, 4, 7, 7, 8, 8, 4, 5, 5, 9, 7, 4, 6,
  17.  
  18. /* .  /  0  1  2  3  4  5  6  7  8  9  : */
  19.    3, 6, 7, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3,
  20.  
  21. /* ;  <  =  >  ?  @  A  B  C  D  E  F  G */
  22.    4, 6, 7, 6, 7, 9, 7, 7, 7, 7, 7, 7, 7,
  23.  
  24. /* H  I  J  K  L  M  N  O  P  Q  R  S  T */
  25.    7, 5, 7, 7, 7,10, 8, 7, 7, 7, 7, 7, 7,
  26.  
  27. /* U  V  W  X  Y  Z  [  \  ]  ^  _  `  a */
  28.    7, 7,11, 7, 7, 7, 5, 6, 5, 7, 7, 4, 7,
  29.  
  30. /* b  c  d  e  f  g  h  i  j  k  l  m  n */
  31.    7, 7, 7, 7, 7, 7, 7, 3, 6, 6, 3, 9, 7,
  32.  
  33. /* o  p  q  r  s  t  u  v  w  x  y  z  { */
  34.    7, 7, 7, 6, 7, 6, 7, 7,11, 7, 7, 7, 6,
  35.  
  36. /* |  }  ~ */
  37.    3, 6, 7};
  38.  
  39. int spacing_14pt[] = {
  40. /* !  "  #  $  %  &  '  (  )  *  +  ,  - */
  41.    4, 7, 9, 8,13, 9, 4, 5, 5, 8, 8, 4, 6,
  42.  
  43. /* .  /  0  1  2  3  4  5  6  7  8  9  : */
  44.    4, 5, 8, 6, 8, 8, 8, 8, 8, 8, 8, 8, 4,
  45.  
  46. /* ;  <  =  >  ?  @  A  B  C  D  E  F  G */
  47.    4, 8, 8, 8, 8,13,10,10, 9,10, 9, 9,10,
  48.  
  49. /* H  I  J  K  L  M  N  O  P  Q  R  S  T */
  50.   10, 4, 7, 9, 9,12,10,10,10,10,11, 9,10,
  51.  
  52. /* U  V  W  X  Y  Z  [  \  ]  ^  _  `  a */
  53.   10,10,16,10,12,11, 5, 6, 5, 7, 9, 6, 8,
  54.  
  55. /* b  c  d  e  f  g  h  i  j  k  l  m  n */
  56.    8, 8, 8, 8, 5, 8, 8, 4, 4, 8, 4,12, 8,
  57.  
  58. /* o  p  q  r  s  t  u  v  w  x  y  z  { */
  59.    8, 8, 8, 7, 8, 5, 8,10,12,10,10, 8, 6,
  60.  
  61. /* |  }  ~ */
  62.    5, 6,12};
  63.  
  64.  
  65. /**********************************************************************\
  66. *                                                                      *
  67. *  center_pstring -- display proportional characters, centered         *
  68. *                                                                      *
  69. \**********************************************************************/
  70.  
  71. void center_pstring(string,x1,x2,y)
  72. char *string;
  73. int x1, x2, y;
  74. {
  75.    int x;
  76.  
  77.    x = get_center(string,x1,x2);
  78.    put_pstring(string,x,y);
  79. }
  80.  
  81.  
  82. /**********************************************************************\
  83. *                                                                      *
  84. *  center_string -- display fixed-pitch characters, centered           *
  85. *                                                                      *
  86. \**********************************************************************/
  87.  
  88. void center_string(string,x1,x2,y)
  89. char *string;
  90. int x1, x2, y;
  91. {
  92.    register int i;
  93.    register int nchar;
  94.    int index;
  95.    int x;
  96.    char ch;
  97.  
  98.    nchar = strlen(string);
  99.    if (nchar == 0) return;
  100.  
  101.    /* calculate center */
  102.  
  103.    x = ((x1 + x2) / 2) - (nchar * 4);
  104.  
  105.    /* display each character as a bitmap */
  106.  
  107.    for (i = 0; i < nchar; i++)
  108.    {
  109.       ch = string[i] - 33;
  110.       if (ch >= 0)
  111.       {
  112.          index = ch * ptsize;
  113.          fg_move(x,y);
  114.          fg_drawmap(&font[index],1,ptsize);
  115.       }
  116.       x += 8;
  117.    }
  118. }
  119.  
  120. /**********************************************************************\
  121. *                                                                      *
  122. *  erase_char -- erase a single fixed-pitch character                  *
  123. *                                                                      *
  124. \**********************************************************************/
  125.  
  126. void erase_char(x,y)
  127. int x, y;
  128. {
  129.    register int color;
  130.  
  131.    /* save the current color */
  132.  
  133.    color = fg_getcolor();
  134.  
  135.    /* draw a black rectangle over the character */
  136.  
  137.    fg_setcolor(0);
  138.    fg_rect(x,x+7,y+1-ptsize,y);
  139.  
  140.    /* set the color back to what it was */
  141.  
  142.    fg_setcolor(color);
  143. }
  144.  
  145. /**********************************************************************\
  146. *                                                                      *
  147. *  first_nonblank -- find first character that is not a blank          *
  148. *                                                                      *
  149. \**********************************************************************/
  150.  
  151. char first_nonblank(string)
  152. char *string;
  153. {
  154.    register int i;
  155.    char c;
  156.  
  157.    i = 0;
  158.    while (TRUE)
  159.    {
  160.       c = string[i++];
  161.       if (c == 0)
  162.          return(0);
  163.       else if (c > 32)
  164.          return(c);
  165.    }
  166. }
  167.  
  168. /**********************************************************************\
  169. *                                                                      *
  170. *  get_center -- locate center for proportionally-spaced characters    *
  171. *                                                                      *
  172. \**********************************************************************/
  173.  
  174. get_center(string,x1,x2)
  175. char *string;
  176. int x1, x2;
  177. {
  178.    return(((x1 + x2) / 2) - (length_pstring(string)/ 2));
  179. }
  180.  
  181. /**********************************************************************\
  182. *                                                                      *
  183. *  get_font -- get the proportional and fixed-pitch character fonts    *
  184. *                                                                      *
  185. \**********************************************************************/
  186.  
  187. void get_font()
  188. {
  189.    register int i;
  190.    register int index;
  191.    int x, y;
  192.    int yoffset;
  193.    char fontfile[10];
  194.  
  195.    /* allocate dynamic memory for the fonts; also define font file name */
  196.  
  197.    if (mode11 || mode16)
  198.    {
  199.       font  = malloc(1316);
  200.       pfont = malloc(2632);
  201.       strcpy(fontfile,"14pt.fnt");   /* high res modes use 14 pt font */
  202.       spacing = spacing_14pt;
  203.       ptsize  = 14;
  204.       yoffset = 18;
  205.    }
  206.    else
  207.    {
  208.       font  = malloc(752);
  209.       pfont = malloc(1504);
  210.       strcpy(fontfile,"08pt.fnt");  /* low res modes use 8 pt font */
  211.       spacing = spacing_08pt;
  212.       ptsize  = 8;
  213.       yoffset = 90;
  214.    }
  215.  
  216.    /* if the allocate failed or the font file isn't present, go no farther */
  217.  
  218.    if (font  == (char *)NULL) abort_program(0);
  219.    if (pfont == (char *)NULL) abort_program(0);
  220.    if (!exists(fontfile))     abort_program(1);
  221.  
  222.    /* display either the 8pt or 14pt fonts on the hidden page */
  223.  
  224.    fg_setpage(hidden);
  225.    fg_move(0,199);
  226.    fg_dispfile(fontfile,320,1);
  227.  
  228.    /* retrieve the proportionally spaced and fixed-pitch fonts */
  229.  
  230.    fg_setcolor(11);
  231.    index = 0;
  232.  
  233.    for (i = 0; i < 94; i++)
  234.    {
  235.       x = 1 + (i/13) * 16;
  236.       y = yoffset + (i%13) * (ptsize+1);
  237.       fg_move(x,y);
  238.       fg_getmap(&pfont[index*2],2,ptsize);  /* put in bitmap array */
  239.       x = 129 + (i/13) * 9;
  240.       fg_move(x,y);
  241.       fg_getmap(&font[index],1,ptsize);
  242.       index += ptsize;
  243.    }
  244. }
  245.  
  246. /**********************************************************************\
  247. *                                                                      *
  248. *  get_string -- accept and display a fixed-pitch character string     *
  249. *                                                                      *
  250. \**********************************************************************/
  251.  
  252. get_string(string,x,y,max_length,string_type)
  253. char *string;
  254. int x, y, max_length, string_type;
  255. {
  256.    register int i;
  257.    int color, timer;
  258.    int xmax, ymin;
  259.    unsigned char key, aux;
  260.  
  261.    /* clear an area for the input string */
  262.  
  263.    fg_setcolor(0);
  264.    xmax = x + 8*max_length;
  265.    ymin = y - ptsize;
  266.    fg_rect(x-2,xmax+1,ymin,y+1);
  267.  
  268.    i = 0;
  269.    timer = 16;
  270.    color = 15;
  271.    fg_setcolor(15);
  272.  
  273.    while (i < max_length)
  274.    {
  275.  
  276.       /* increment the timer to make the cursor blink */
  277.  
  278.       timer--;
  279.       if (timer == 8)
  280.          color = 0;
  281.       else if (timer == 0)
  282.       {
  283.          timer = 16;
  284.          color = 15;
  285.       }
  286.       put_cursor(x,y,color);
  287.  
  288.       /* pause, then get a keystroke */
  289.  
  290.       fg_waitfor(1);
  291.       fg_intkey(&key,&aux);
  292.  
  293.       /* printable character */
  294.  
  295.       if (isalnum(key) || ((string_type != ALPHANUMERIC) && ispunct(key)))
  296.       {
  297.          put_cursor(x,y,0);
  298.          put_char(key,x,y);
  299.          x += 8;
  300.          string[i++] = key;
  301.       }
  302.  
  303.       /* backspace -- delete a character */
  304.  
  305.       else if (key == BS && i > 0)
  306.       {
  307.          put_cursor(x,y,0);
  308.          x -= 8;
  309.          erase_char(x,y);
  310.          i--;
  311.       }
  312.  
  313.       /* ESC key terminates input */
  314.  
  315.       else if (key == ESC)
  316.          return(ESC);
  317.  
  318.       /* carriage return terminates input */
  319.  
  320.       else if (key == CR)
  321.          break;
  322.    }
  323.  
  324.    /* zero-length string? */
  325.  
  326.    if (i == 0) return(ESC);
  327.  
  328.    /* string terminator */
  329.  
  330.    string[i] = '\0';
  331.    return(OK);
  332. }
  333.  
  334. /**********************************************************************\
  335. *                                                                      *
  336. *  length_pstring -- compute the length of a proportional string       *
  337. *                                                                      *
  338. \**********************************************************************/
  339.  
  340. length_pstring(string)
  341. char *string;
  342. {
  343.    register int i;
  344.    register int nchar;
  345.    int length;
  346.    char ch;
  347.  
  348.    /* number of characters in the string */
  349.  
  350.    nchar = strlen(string);
  351.    if (nchar == 0) return(0);
  352.  
  353.    length = 0;
  354.  
  355.    /* total up pixel width of each character */
  356.  
  357.    for (i = 0; i < nchar; i++)
  358.    {
  359.       ch = string[i] - 33;
  360.       if (ch < 0)
  361.          length += 8;
  362.       else
  363.          length += spacing[ch];
  364.    }
  365.  
  366.    return(length);
  367. }
  368.  
  369. /**********************************************************************\
  370. *                                                                      *
  371. *  put_char -- display a single fixed-pitch character                  *
  372. *                                                                      *
  373. \**********************************************************************/
  374.  
  375. void put_char(ch,x,y)
  376. char ch;
  377. int x, y;
  378. {
  379.    register int index;
  380.  
  381.    /* 32 is space, 33 is first printable ASCII character */
  382.  
  383.    ch -= 33;
  384.    if (ch >= 0)
  385.    {
  386.       /* calculate index into the bitmap array */
  387.  
  388.       index = ch * ptsize;
  389.  
  390.       /* draw the character as a mode independent bitmap */
  391.  
  392.       fg_move(x,y);
  393.       fg_drawmap(&font[index],1,ptsize);
  394.    }
  395. }
  396.  
  397. /**********************************************************************\
  398. *                                                                      *
  399. *  put_cursor -- display the cursor line                               *
  400. *                                                                      *
  401. \**********************************************************************/
  402.  
  403. void put_cursor(x,y,cursor_color)
  404. int x, y;
  405. int cursor_color;
  406. {
  407.    register int color;
  408.  
  409.    /* preserve current foreground color */
  410.  
  411.    color = fg_getcolor();
  412.  
  413.    /* draw the cursor */
  414.  
  415.    fg_setcolor(cursor_color);
  416.    fg_rect(x,x+7,y,y);
  417.  
  418.    /* restore the foreground color */
  419.  
  420.    fg_setcolor(color);
  421. }
  422.  
  423. /**********************************************************************\
  424. *                                                                      *
  425. *  put_pstring -- display proportionally-spaced characters             *
  426. *                                                                      *
  427. \**********************************************************************/
  428.  
  429. void put_pstring(string,x,y)
  430. char *string;
  431. int x, y;
  432. {
  433.    register int i;
  434.    register int nchar;
  435.    int index;
  436.    int twice_ptsize;
  437.    char ch;
  438.  
  439.    /* get the length of the string */
  440.  
  441.    nchar = strlen(string);
  442.    if (nchar == 0) return;
  443.  
  444.    twice_ptsize = ptsize * 2;
  445.  
  446.    /* adjust y coordinate for screen resolution (it looks better) */
  447.  
  448.    if (ptsize == 8) y--;
  449.  
  450.    /* display each character as a mode-independent bitmap */
  451.  
  452.    for (i = 0; i < nchar; i++)
  453.    {
  454.       ch = string[i] - 33;
  455.  
  456.       /* printable character: greater than ASCII 32 */
  457.  
  458.       if (ch >= 0)
  459.       {
  460.          index = ch * twice_ptsize;
  461.          fg_move(x,y);
  462.          fg_drawmap(&pfont[index],2,ptsize);
  463.  
  464.          /* increment x depending on the proportional space */
  465.  
  466.          x += spacing[ch];
  467.       }
  468.  
  469.       /* assume that white space will be 8 pixels wide */
  470.  
  471.       else
  472.          x += 8;
  473.    }
  474. }
  475.  
  476. /**********************************************************************\
  477. *                                                                      *
  478. *  put_string -- display fixed-pitch characters                        *
  479. *                                                                      *
  480. \**********************************************************************/
  481.  
  482. void put_string(string,x,y)
  483. char *string;
  484. int x, y;
  485. {
  486.    register int i;
  487.    register int nchar;
  488.    int index;
  489.    char ch;
  490.  
  491.    /* determine the length of the string */
  492.  
  493.    nchar = strlen(string);
  494.    if (nchar == 0) return;
  495.  
  496.    for (i = 0; i < nchar; i++)
  497.    {
  498.       ch = string[i] - 33;
  499.       if (ch >= 0)
  500.       {
  501.          index = ch * ptsize;
  502.          fg_move(x,y);
  503.          fg_drawmap(&font[index],1,ptsize);
  504.       }
  505.  
  506.       /* always increment by 8, not proportionally spaced */
  507.  
  508.       x += 8;
  509.    }
  510. }
  511.  
  512. /**********************************************************************\
  513. *                                                                      *
  514. *  put_tstring -- display ROM BIOS text                                *
  515. *                                                                      *
  516. \**********************************************************************/
  517.  
  518. void put_tstring(string,row,col)
  519. char *string;
  520. int row, col;
  521. {
  522.    /* character space is defined in rows and columns */
  523.  
  524.    fg_locate(row,col);
  525.  
  526.    /* ROM text */
  527.  
  528.    fg_text(string,strlen(string));
  529. }
  530.  
  531. /**********************************************************************\
  532. *                                                                      *
  533. *  row_offset -- return the row offset for any font                    *
  534. *                                                                      *
  535. \**********************************************************************/
  536.  
  537. row_offset(nrows)
  538. int nrows;
  539. {
  540.    return(nrows*ptsize);
  541. }
  542.